package com.qijian.mold.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.mold.domain.dto.*;
import com.qijian.mold.domain.entity.MoldChange;
import com.qijian.mold.domain.vo.moldChange.EqpVo;
import com.qijian.mold.domain.vo.moldChange.MoldChangeDownVo;
import com.qijian.mold.domain.vo.moldChange.MoldChangeListVo;
import com.qijian.mold.domain.vo.moldChange.WorkOrderDetailsVo;
import com.qijian.mold.domain.vo.user.UserDetailsVo;
import com.qijian.mold.mapper.MoldChangeMapper;
import com.qijian.mold.repository.MoldChangeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 换模信息
 *
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2025/3/29 17:25
 */
@Service
public class MoldChangeRepositoryImpl extends ServiceImpl<MoldChangeMapper, MoldChange> implements MoldChangeRepository {
    @Autowired
    private MoldChangeMapper mapper;

    @Override
    public EqpVo getEqpDetail(Long eqpId) {
        return mapper.getEqpDetail(eqpId);
    }

    @Override
    public EqpVo getEqpByCode(String code) {
        List<EqpVo> eqpByCode = mapper.getEqpByCode(code);
        if (CollectionUtil.isNotEmpty(eqpByCode)) {
            return eqpByCode.get(0);
        }
        return null;
    }

    @Override
    public WorkOrderDetailsVo getWorkOrderDetails(Long workOrderDetailId) {
        WorkOrderDetailsVo vo = mapper.getWorkOrderDetails(workOrderDetailId);
        if (vo != null) {
            List<String> nameList = mapper.getWorkOrderDetailFlowByOrderId(vo.getWorkOrderId());
            String name = nameList.stream().collect(Collectors.joining(">"));
            vo.setWorkOrderDetailFlow(name);
        }
        return vo;
    }

    @Override
    public Page<MoldChangeListVo> getByPage(SelectMoldChangeDto dto) {
        return mapper.getByPage(dto);
    }

    @Override
    public Page<MoldChangeDownVo> getMoldChangeListByCurrentId(GetMoldChangeListByCurrentIdDto dto) {
        return mapper.getMoldChangeListByCurrentId(dto);
    }

    @Override
    public Page<EqpVo> getEqpList(GetEqpDto dto) {
        return mapper.getEqpList(dto);
    }

    @Override
    public Page<WorkOrderDetailsVo> getWorkOrderDetailsList(GetWorkOrderDetailsListDto dto) {
        Page<WorkOrderDetailsVo> detailsList = mapper.getWorkOrderDetailsList(dto);
//        if (CollectionUtil.isNotEmpty(detailsList)) {
//            detailsList.forEach(workOrderDetailsVo -> {
//                List<String> nameList = mapper.getWorkOrderDetailFlowByOrderId(workOrderDetailsVo.getWorkOrderId());
//                String name = nameList.stream().collect(Collectors.joining(">"));
//                workOrderDetailsVo.setWorkOrderDetailFlow(name);
//            });
//        }
        return detailsList;
    }

    /**
     * 查看该用户是不是对应的角色
     *
     * @param userId
     * @return
     */
    @Override
    public Boolean getMoleRoleByUserId(Long userId, String roleKey) {
        List<Long> moleAdminRoleByUserId = mapper.getMoleRoleByUserId(userId, roleKey);
        if (CollectionUtil.isNotEmpty(moleAdminRoleByUserId)) {
            return true;
        }
        return false;
    }

    /**
     * 根据角色key获取用户
     *
     * @param
     * @return
     */
    @Override
    public Page<UserDetailsVo> getUserByRoleKey(GetUserDetailsByRoleTypeDto dto) {
        return mapper.getUserByRoleKey(dto);
    }

    /**
     * 根据ids获取用户
     * @param ids
     * @return
     */
    @Override
    public List<UserDetailsVo> getUserListByIds(List<Long> ids) {
        return mapper.getUserListByIds(ids);
    }

    @Override
    public Page<UserDetailsVo> getOperatorByRoleKey(GetUserDetailsByRoleTypeDto dto) {
        return mapper.getOperatorByRoleKey(dto);
    }

    @Override
    public Boolean getOperatorByLocationId(String roleKey, Long locationId, Long userId) {
        List<Long> moleAdminRoleByUserId = mapper.getOperatorByLocationId(roleKey, locationId, userId);
        if (CollectionUtil.isNotEmpty(moleAdminRoleByUserId)) {
            return true;
        }
        return false;
    }

}
